package org.lq.shopping.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.log4j.Logger;
import org.lq.shopping.dao.Shop_collectionDao;
import org.lq.shopping.entity.Shop_collection;
import org.lq.shopping.test.LogTest;
import org.lq.shopping.util.DruidUtil;

public class Shop_collectionDaoImpl implements Shop_collectionDao{
	Logger log = Logger.getLogger(Shop_collection.class);
	QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
	@Override
	public int add(Shop_collection t) {
		int num = 0;
		String sql = "insert into shop_collection" + 
				"  (goods_id, user_id)" + 
				"values " + 
				" (?, ?)";
		log.debug("传入"+t.toString());
		log.debug(sql);
		try {
			num = qr.execute(sql, t.getGoods_id(),t.getUser_id());
		} catch (SQLException e) {
			log.error("商品收藏错误异常");
		}
		return num;
	}

	@Override
	
	public int delete(int id) {
		int num = 0 ;
		String sql = "delete shop_collection where goods_id = ?";
		log.debug("传入"+id);
		log.debug(sql);
		try {
			num = qr.execute(sql, id);
		} catch (SQLException e) {
			log.error("商品收藏错误异常");
		}
		return num;
	}

	@Override
	public int update(Shop_collection t) {
		int num = 0;
		String sql = "update shop_collection" +
						" set user_id = ?" + 
						" where goods_id = ?";
		log.debug("传入"+t.toString());
		log.debug(sql);
		try {
			num = qr.execute(sql, t.getUser_id(),t.getGoods_id());
		} catch (SQLException e) {
			log.error("商品收藏错误异常");
		}
		return num;
	}

	@Override
	public List<Shop_collection> findAll() {
		List<Shop_collection> list = null;
		String sql = "select * from shop_collection";
		log.debug("传入语句"+sql);
		try {
			list = qr.query(sql, new BeanListHandler<>(Shop_collection.class));
		} catch (SQLException e) {
			log.error("商品收藏错误异常");
		}
		return list;
	}

	@Override
	public Shop_collection findById(int id) {
		Shop_collection sc = null;
		String sql = "select * from shop_collection where goods_id=?";
		log.debug("传入"+id);
		log.debug(sql);
		try {
			sc = qr.query(sql, new BeanHandler<>(Shop_collection.class),id);
		} catch (SQLException e) {
			log.error("商品收藏错误异常");
		}
		return sc;
	}

	@Override
	public List<Shop_collection> findPage(int pageIndex, int pageSize) {
		List<Shop_collection> list = null;
		String sql = "select * from"
				+ "(select rownum r,s.* from "
				+ "(select * from shop_collection order by goods_id) s "
				+ "where rownum <= ?)"
				+ " where r>?";
		log.debug("传入:页数"+pageIndex+"行数"+pageSize);
		log.debug(sql);
		int max = pageIndex * pageSize;
		int min = (pageIndex-1)*pageSize;
		try {
			list = qr.query(sql, new BeanListHandler<>(Shop_collection.class),max,min);
		} catch (SQLException e) {
			log.error("商品收藏错误异常");
		}
		return list;
	}

	@Override
	public List<Shop_collection> LikeColumnByValue(String columName, Object value) {
		List<Shop_collection> list = null;
		String sql = "select * from shop_collection where goods_id like ?";
		log.debug("传入:"+columName);
		log.debug(sql);
		try {
			list = qr.query(sql, new BeanListHandler<>(Shop_collection.class),"%"+columName+"%");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int getCount() {
		int count = 0 ;
		String sql = "select count(1) from shop_collection";
		log.debug(sql);
		try {
			count = qr.query(sql, new ScalarHandler<BigDecimal>()).intValue();
		} catch (SQLException e) {
			log.error("商品收藏类错误异常");
		}
		return count;
	}

	@Override
	public List<Shop_collection> findbyuserid(int userid) {
		List<Shop_collection> list = null;
		String sql = "select * from shop_collection where user_id = ? ";
		log.debug("传入语句"+sql);
		try {
			list = qr.query(sql, new BeanListHandler<>(Shop_collection.class),userid);
		} catch (SQLException e) {
			log.error("商品收藏错误异常");
		}
		return list;
	}

	

	}



